<template>
	<view class="content">
		<view class="nav-top">
			<uni-search :hascity="false" :show="true" @confirm="search" v-model="params.name"></uni-search>
			<view class="nav-classify">
			    <view v-for="tab,index in tabBars" :key="index" :data-order="tab.id" class="uni-tab-item" :id="tab.id" @click="ontabtap">
			        <text class="uni-tab-item-title" :class="tabOrder==tab.id ? 'uni-tab-item-title-active' : ''">
						{{tab.name}}
					</text>
			    </view>
				<view class="uni-tab-item" @click="ontabtap" data-order="minprice">
					<view class="uni-tab-item-title">
						<text :class="tabOrder=='minprice' ? 'uni-tab-item-title-active' : ''">
							价格
						</text>
						<image class="image-price" mode="widthFix" :src="'../../static/image/icon/listsort'+ listorder + '.png'" />
					</view>
				</view>
				<view class="uni-tab-item" data-order="filter" @click="showFilter">
					<text class="uni-tab-item-title">
						筛选
						<text class="uni-text-small icon iconfont icon-filter"></text>
					</text>
				</view>
			</view>
		</view>
		<view class="product-list">
			<product-list :list-data="listData" @imgClick="goDetail"></product-list>
			<uni-load-more :status="statusByhasMore"  :icon-size="16" :content-text="contentText" />
		</view>
		
		<view class="filter-content" :class="filterFlag?'on':''">
			<view class="filter-item">
				<text class="filter-text">
					出游天数(多选)
				</text>
				<view class="days">
					<view class="days-item filter-on"  v-for="item,index in filterDays" :key = "index">
						{{item}} 天
					</view>
				</view>
			</view>
			<view class="filter-item">
				<text class="filter-text">
					价格预算
				</text>
				<view class="days">
					<view class="days-item" v-for="item,index in budgets" :key="index">
						{{item}}
					</view>
				</view>
				<!-- <view>
					最低价----最高价
				</view> -->
			</view>
			<view class="filter-item">
				<text class="filter-text">
					热门搜索
				</text>
				<view class="days">
					<view class="days-item" v-for="item,index in hotItems" :key = "index">
						{{item}}
					</view>
				</view>
			</view>
			<view class="filter-item btns">
				<view class="btn-item">
					重置
				</view>
				<view class="btn-item btn-submit" @click="filterSubmit">
					确定
				</view>
			</view>
		</view>
		
		<view class="uni-mask" @click="showFilter" :class="{show:maskshow}"></view>
	</view>
</template>

<script>
	import uniSearch from "@/components/search/search.vue";
	import uniIcons from "@/components/uni-icons/uni-icons.vue";
	import productList from "@/pages/product/components/product-item.vue"
	// 缓存每页最多
	const MAX_CACHE_DATA = 100;
	// 缓存页签数量
	const MAX_CACHE_PAGE = 3;
	export default {
		components:{
			uniSearch,
			uniIcons,
			productList
		},
		data() {
			return {
				filterBtns:{},
				maskshow:false,
				filterDays:[6,7,8,9],
				budgets:['0-3000','3000-5000','5000-10000','10000以上'],
				hotItems:['漂流','方特','水上乐园','欧乐堡济南','温泉','济南动物园','动物园','南部山区','台儿庄'],
				params:{
					order:'',
					by:'',
					name:'',
					pageIndex:1
				},
				page:{
					pageIndex:1
				},
				hasMore:true,
				filterFlag:false,
				listorder:'',
				reload: false,
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多了'
				},
				listData:[],
				newsObj: {
					"search":{
						data:[],
						page:{
							pageIndex:1
						}
					},
					"all":{
						data:[],
						page:{
							pageIndex:1
						}
					},
					"sales":{
						data:[],
						page:{
							pageIndex:1
						}
					},
					"newp":{
						data:[],
						page:{
							pageIndex:1
						}
					},
					"minprice":{
						data:[],
						page:{
							pageIndex:1
						}
					},
					"filter":{
						data:[],
						page:{
							pageIndex:1
						}
					}
				},
				cacheTab: [],
				tabOrder: "",
				tabBars: [
					{
						name: '综合',
						id: ''
					}, 
					{
						name: '销量',
						id: 'sales'
					}, 
					{
						name: '新品',
						id: 'newp'
					}
				]
			}
		},
		onLoad() {
			this.getList();
		},
		computed:{
			statusByhasMore(){
				return this.hasMore?(this.hasMore=='loading'?'loading':'more'):'nomore'
			},
			activeTab(){
				return this.newsObj[this.tabOrder==""?"all":this.tabOrder];
			}
		},
		onPullDownRefresh() {
			// 下拉刷新
			this.reload = true;
			this.activeTab.page.pageIndex=1;
			this.getList();
			setTimeout(function(){
				uni.stopPullDownRefresh()
			},500)
		},
		onReachBottom() {
			if(this.hasMore){
				this.activeTab.page.pageIndex+=1;
				this.getList();
			}
		},
		methods: {
			search(){
				this.params.order="";
				this.tabOrder = 'search';
				this.reload = true;
				this.getList();
			},
			getList(){
				this.hasMore="loading";
				let activeTab = this.activeTab;
				this.params.pageIndex = activeTab.page.pageIndex;
				uniCloud.callFunction({
					name:"getProduct",
					data:this.params
				}).then(res=>{
					const {data,hasMore} = res.result;
					this.hasMore = hasMore;
					activeTab.data = this.reload ? data : activeTab.data.concat(data);
					this.listData = activeTab.data
					if(this.reload){
						this.reload = false
					}
				}).catch((err)=>{
					uni.showModal({
						content: `调用云函数失败，错误信息为：${err.message}`,
						showCancel: true
					})
				})
			},
			ontabtap(e) {
				let selorder = (e.target.dataset.hasOwnProperty('order'))?(e.target.dataset.order):(e.currentTarget.dataset.order);
				this.switchTab(selorder);
			},
			switchTab(e) {
				const a = this.params;
				this.params.name="";
				if("minprice" == e){
					a.order == e?"desc" == a.by?a.by="asc":a.by = "desc":a.by="asc";
					a.order = e;
					this.newsObj['minprice'].data = [];
					this.newsObj['minprice'].page.pageIndex = 1;
					this.listorder=a.by;
				}else{
					a.order == e?'':a.order = e;
					this.listorder="";
					"sales" == e?a.by="desc":a.by="";
				}
				this.tabOrder === e?'':this.tabOrder = e;
				if (this.newsObj[a.order==""?"all":a.order].data.length === 0) {
				    this.getList();
				}else{
					this.listData = this.newsObj[a.order==""?"all":a.order].data;
				}
			},
			
			goDetail(val){
				// 前往详情页
				uni.navigateTo({
					url:'/pages/classify/detail?id=1'
				})
			},
			showFilter(){
				// 显示过滤窗
				this.filterFlag = !this.filterFlag;
				this.maskshow = !this.maskshow;
			},
			filterSubmit(){
				// console.log('过滤')
			}
		}
	}
</script>

<style>
	.nav-top{
		position: fixed;
		top:  var(--window-top);
		z-index: 13;
		width: 100%;
		background-color: #fff;
	}
	
	.product-list{
		margin-top: 190rpx;
	}
	
	.nav-classify {
		width: 100%;
        height: 80rpx;
		display: flex;
        flex-direction: row;
		justify-content: space-around;
        /* #ifndef APP-PLUS */
        white-space: nowrap;
        /* #endif */
        /* flex-wrap: nowrap; */
        border-color: #cccccc;
		border-bottom-style: solid;
		border-bottom-width: 1px;
    }
    
	.uni-tab-item {
        /* #ifndef APP-PLUS */
        display: inline-block;
        /* #endif */
        flex-wrap: nowrap;
        padding-left: 34rpx;
        padding-right: 34rpx;
    }

    .uni-tab-item-title {
        color: #555;
        font-size: 30rpx;
        height: 80rpx;
        line-height: 80rpx;
        flex-wrap: nowrap;
        /* #ifndef APP-PLUS */
        white-space: nowrap;
        /* #endif */
    }

    .uni-tab-item-title-active {
        color: #fc901e;
    }
	
	.uni-text-small{
		font-size: 24rpx !important;
	}
	
	.image-price{
		vertical-align: text-top;
		height: 36rpx;
		width: 36rpx;
	}
	
	/* 过滤选择框 */
	.filter-content{
		background-color: #fff;
		width: 100%;
		z-index: 12;/* 高于mask的index值 */
		position: fixed;
		top:calc(186rpx + var(--window-top));
		left: 0;
		opacity: 0;
		transition-property: height,opacity,transform,-webkit-transform;
		transition-duration: 300ms;
		transform: translate3d(0px,-100%,0px) scaleY(0);
	}
	
	.filter-content.on{
		transition-duration: 0.3s;
		opacity: 1;
		height: auto;
		transform: translate3d(0px,0px,0px) scaleY(1);
	}
	
	.filter-item{
		margin-top: 20rpx;
		padding: 0 10rpx;
	}
	
	.filter-item.btns{
		margin-top: 80rpx;
		padding:0;
	}
	
	.filter-text{
		color: #888;
		padding-left: 15rpx;
	}
	
	.days{
		text-align: left;
	}
	
	.days-item{
		border: 1px solid #999;
		padding: 5rpx;
		width: 140rpx;
		border-radius: 10rpx;
		display: inline-block;
		text-align: center;
		margin: 10rpx;
		font-size: 24rpx;
		/* box-shadow: 2rpx 2rpx 2rpx #2C405A; */
	}
	
	.budget-item{
		border: 1px solid #999;
		padding: 5rpx;
		width: 140rpx;
		border-radius: 10rpx;
		display: inline-block;
		text-align: center;
		margin: 10rpx;
		font-size: 24rpx;
	}
	.btn-item{
		border-top: 1px solid #999;
		padding: 10rpx 0;
		display: inline-block;
		width: 50%;
		text-align: center;
		color: #808080;
	}
	.btn-submit{
		background: #ef791d;
		color: #fff;
	}
	.show{
		display: block;
	}
	.filter-on{
		border: 1px solid #4cd964;
		color: #4cd964;
	}
</style>
